$(function () {
    var formalStudent_datagrid = $("#formalStudent_datagrid");
    var formalStudent_dialog = $("#formalStudent_dialog");
    var formalStudent_form = $("#formalStudent_form");
    var class_dialog = $("#class_dialog");
    var class_form = $("#class_form");
    var drain_dialog = $("#drain_dialog");
    var drain_form = $("#drain_form");
    formalStudent_datagrid.datagrid({
        url: '/formalStudent/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'name', title: '学员姓名', width: 100},
            {
                field: 'salemen', title: '营销人员', width: 100, formatter: function (value) {
                return value.username;
            }
            },
            {field: 'totalTuition', title: '总学费', width: 100,},
            {field: 'debt', title: '待缴学费', width: 100,},
            {field: 'prepaidTuition', title: '已交学费', width: 100,},
            {field: 'completePayment', title: '缴费状态', width: 100,formatter:function (value) {
                return value?'<font color="green">已缴清</font>' : '<font color="red">未缴清</font>'
            }},
            {field: 'entranceTime', title: '入学时间', width: 100,},
            {field: 'schoolName', title: '学校', width: 100,},
            {field: 'tel', title: '电话', width: 100,},
            {
                field: 'intentionClass', title: '所在班级', width: 100, formatter: function (value) {
                return value.className;
            }
            },
            {
                field: 'customerType', title: '类型', width: 100, formatter: function (value) {
                return value.name;
            }
            },
            {
                field: 'status', title: '状态', width: 100, formatter: function (value) {
                if (value == 0) {
                    return '正常';
                } else if (value == 1) {
                    return '<font color="orange">转班中</font>';
                } else if (value == 3) {
                    return '<font color="red">休学中</font>';
                }
            }
            },
        ]],
        singleSelect: true,
        rownumbers: true,
        pagination: true,
        toolbar: "#formalStudent_btns",
        sortName: 'hireDate',
        sortOrder: 'desc'
    });
    formalStudent_dialog.dialog({
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            formalStudent_form.form('clear');
        }
    });
    drain_dialog.dialog({
        title:'学员流失',
        width:350,
        height:300,
        buttons: "#bandon_btns",
        closed: true,
        onClose: function () {
            drain_form.form('clear');
        }
    });
    class_dialog.dialog({
        title: '转班',
        width: 300,
        height: 200,
        buttons: "#change_btns",
        closed: true,
        onClose: function () {
            class_form.form('clear');
        }
    });
    var methodObj = {
        abandon:function () {
            var row = formalStudent_datagrid.datagrid("getSelected");
            if(row.status==1){
                $.messager.alert('温馨提示', "该学员正在转班,无法流失", 'error');
                return;
            }else if(row.status==2){
                $.messager.alert('温馨提示', "该学员正在休学,无法流失", 'error');
                return;
            }
            var row = formalStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            drain_form.form('load',row);
            drain_dialog.dialog('open');
        },
        save_bandon:function () {
            drain_form.form('submit',{
                url:'/formalStudent/bandon.do',
                onSubmit:function (param) {
                    var formalStudentId = $("#drain_form [name=id]").val();
                    param['formalStudentId'] = formalStudentId;
                    console.log(formalStudentId);
                },
                success:function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '流失成功', 'info')
                        methodObj.cancel_bandon();
                        formalStudent_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        cancel_bandon:function () {
            drain_dialog.dialog('close');
        },
        cancel_class: function () {
            class_dialog.dialog('close');
        },
        save_class: function () {
            var row = formalStudent_datagrid.datagrid("getSelected");
            class_form.form('submit', {
                url: '/formalStudent/transfer.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '转班成功', 'info')
                        methodObj.cancel_class();
                        formalStudent_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        changeClass: function () {
            var row = formalStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            if(row.status == 1){
                $.messager.alert('温馨提示', "该学员处于转班中,无法再次转班", 'warning');
                return;
            }
            if(row.status == 2){
                $.messager.alert('温馨提示', "该学员流失了,无法转班", 'warning');
                return;
            }
            row['intentionClass.id'] = row.intentionClass.id;
            console.log(row['intentionClass.id']);
            class_form.form('load', row);
            class_dialog.dialog('open');
        },
        watch: function () {
            var row = formalStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            row['paymentType.id'] = row.paymentType.id;
            row['salemen.id'] = row.salemen.id;
            row['intentionClass'] = row.intentionClass.id;
            row['customerType.id'] = row.customerType.id;
            $("#formalStudent_form input").prop('disabled', true);
            $("#formalStudent_form input").prop('readOnly', true);
            formalStudent_form.form('load', row
            );
            formalStudent_dialog.dialog('setTitle', '查看正式学员');
            $("#save").hide();
            formalStudent_dialog.dialog('open');
        },
        edit: function () {
            $("#save").show();
            var row = formalStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            var intentionClass = row.intentionClass;
            row['paymentType.id'] = row.paymentType.id;
            row['salemen.id'] = row.salemen.id;
            row['intentionClass.id'] = intentionClass.id;
            row['customerType.id'] = row.customerType.id;
            formalStudent_form.form('load', row
            );
            row['intentionClass'] = intentionClass;
            formalStudent_dialog.dialog('setTitle', '编辑正式学员');
            formalStudent_dialog.dialog('open');
        },
        del: function () {
            var row = formalStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该正式学员吗？', function (r) {
                if (r) {
                    $.get("/formalStudent/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            formalStudent_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            formalStudent_datagrid.datagrid('reload');
        },

        save: function () {
            formalStudent_form.form('submit', {
                url: '/formalStudent/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        formalStudent_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            formalStudent_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
})



